using System.Collections;
using Concurso;
using Curriculum.Services;
using Curriculum.Utiles;
using Utiles;

namespace ConcursoCV.UI.WebUserControls.TipoLista
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;

	/// <summary>
	///		Summary description for ListadoTipoLista.
	/// </summary>
	public class ListadoTipoLista : System.Web.UI.UserControl
	{
		protected System.Web.UI.WebControls.Button btnBuscar;
		protected System.Web.UI.WebControls.Button btnLimpiar;
		protected System.Web.UI.WebControls.Label lblCount;
		protected System.Web.UI.WebControls.DataGrid dtgTipoLista;
		protected System.Web.UI.WebControls.Button btnNew;
		protected System.Web.UI.WebControls.DropDownList ddlAntecedente;
		protected System.Web.UI.WebControls.DropDownList ddlAtributo;

		private const string pidABMTipoLista = "017";		

		private void Page_Load(object sender, System.EventArgs e)
		{
			if(!this.IsPostBack)
			{				
				InitializeVariables();
				PopulateCombo();				
			}
		}
		private void PopulateCombo()
		{
			PopulateComboAntecedente();
			PopulateComboAtributo();
		}
		private void PopulateComboAntecedente()
		{
			VersionMetadatosService svc = new VersionMetadatosService();			
			FillSearchVariables();
			Helper.ListHelper.FillDropDownList( ddlAntecedente, "id", "nombre", svc.ObtenerAntecedentesMetadatoConAtributoTipoLista(), true );
		}

		private void PopulateComboAtributo()
		{
			VersionMetadatosService svc = new VersionMetadatosService();
			FillSearchVariables();
			Helper.ListHelper.FillDropDownList( ddlAtributo, "id", "nombre", svc.ObtenerAtributosTipoLista(), true );
		}

		private void InitializeVariables()
		{
			SortColumn = "AntecedenteMetadato";
			SortDirection = true;			
		}

		private void FillSearchVariables() 
		{
			FiltroAntecedente = ddlAntecedente.SelectedIndex > 0 ? ddlAntecedente.SelectedValue  : string.Empty;
			FiltroAtributo = ddlAtributo.SelectedIndex > 0 ? ddlAtributo.SelectedValue  : string.Empty;
		}

		private IList GetListBySearch() 
		{
			// Search			
			VersionMetadatosService service = new VersionMetadatosService();
			return service.GetListaValores( FiltroAntecedente, FiltroAtributo);
		}


		private void FillDataGridWithSearch()
		{
			IList list = GetListBySearch();
			((ArrayList)list).Sort(new GenericComparer(SortColumn,SortDirection));
			dtgTipoLista.DataSource = list;
			lblCount.Text = list.Count.ToString();
			try
			{
				dtgTipoLista.DataBind();
			}
			catch(Exception exc )
			{
				string msg = exc.Message;
				dtgTipoLista.CurrentPageIndex = 0;
				FillDataGridWithSearch();
			}

			list = null;
		}


		private void RedirectToABMTipoLista(PageMode mode, string itemListaValores)
		{
			string url = "Default.aspx?pid=" + pidABMTipoLista + "&itemListaValoresId=" + itemListaValores + "&PageMode=" + PageModeConverter.ToInt( mode );
			Server.Transfer(url);
		}

		private void EliminarItemListaValores(string itemListaValoresId)
		{
			VersionMetadatosService svc = new VersionMetadatosService();
			svc.EliminarItemListaValores(itemListaValoresId);
		}

		#region - properties -

		public string FiltroAtributo
		{
			get{ return (string)ViewState["_filtroAtributoId"];}
			set{ ViewState["_filtroAtributoId"] = value;}
		}
		public string FiltroAntecedente
		{
			get{ return (string)ViewState["_filtroAntecedenteId"];}
			set{ ViewState["_filtroAntecedenteId"] = value;}
		}

		#region - Sort Variables -

		public string SortColumn
		{
			get{ return (string)ViewState["SortColumn"];}
			set{ ViewState["SortColumn"] = value;}
		}
		public bool SortDirection
		{
			get{ return (bool)ViewState["SortDirection"];}
			set{ ViewState["SortDirection"] = value;}
		}

		#endregion - Sort Variables -

		#endregion - properties -

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.btnBuscar.Click += new System.EventHandler(this.btnBuscar_Click);
			this.btnLimpiar.Click += new System.EventHandler(this.btnLimpiar_Click);
			this.dtgTipoLista.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dtgTipoLista_ItemCommand);
			this.dtgTipoLista.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.dtgTipoLista_PageIndexChanged);
			this.dtgTipoLista.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.dtgTipoLista_SortCommand);
			this.btnNew.Click += new System.EventHandler(this.btnNew_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void dtgTipoLista_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			if ( e.CommandName != "Page" ) 
			{
				string itemListaValoresId = e.Item.Cells[0].Text;				
				switch ( e.CommandName ) 
				{
					case "View":
						RedirectToABMTipoLista(PageMode.VIEW,itemListaValoresId);
						break;

					case "Modify":
						RedirectToABMTipoLista(PageMode.EDIT,itemListaValoresId);
						break;
						
					case "Delete":
						EliminarItemListaValores(itemListaValoresId);	
						FillDataGridWithSearch();
						break;
				}
			}			
		
		}

		private void dtgTipoLista_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
		{
			dtgTipoLista.CurrentPageIndex = e.NewPageIndex;
			FillDataGridWithSearch();		
		}

		private void dtgTipoLista_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
		{
			//Si se hace click sobre ,la misma columna, cambia el orden
			if (SortColumn.ToString() == e.SortExpression.ToString())
				SortDirection = !SortDirection;
			
			SortColumn = e.SortExpression;
			FillDataGridWithSearch();		
		}

		private void btnBuscar_Click(object sender, System.EventArgs e)
		{
			dtgTipoLista.CurrentPageIndex=0;
			FillSearchVariables();
			FillDataGridWithSearch();		
		}

		private void btnLimpiar_Click(object sender, System.EventArgs e)
		{
			ddlAntecedente.SelectedValue = "0";
			ddlAtributo.SelectedValue = "0";
		}

		private void btnNew_Click(object sender, System.EventArgs e)
		{
			string url = "Default.aspx?pid=" + pidABMTipoLista;
			Server.Transfer(url);		
		}
	}
}
